package com.zhiyou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.entity.Lessee;
import com.zhiyou.service.LesseeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;


@Controller
@RequestMapping("/lessee")
public class LesseeController {
    @Autowired
    LesseeService lesseeService;

    //查询所有租户信息
    @RequestMapping("/getAll")
    public String getAll(Model model,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        PageHelper.startPage(pageNum,2);
        List<Lessee> lesseeList = lesseeService.getAll();
        PageInfo<Lessee> pageInfo=new PageInfo<>(lesseeList,5);
        model.addAttribute("list",pageInfo);//保存查询到的租户信息
        model.addAttribute("lesseeList",lesseeList);
        return "lesseelist";
    }
    //根据条件模糊查询租户信息
    @RequestMapping("/getLesseeLikeAll")
    public String getLesseeLikeAll(@RequestParam(value = "likes") String likes,Model model){
        List<Lessee> lesseeLikeAll = lesseeService.getLesseeLikeAll(likes);
        if(lesseeLikeAll.size()!=0){
            model.addAttribute("lesseeList",lesseeLikeAll);//保存查询到的租户信息
            return "lesseelist";
        }
        return "noSeleteLessee";
    }
    //根据id获取要修改的租户信息
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Model model){
        Lessee lessee = lesseeService.selectLesseeById(id);
        model.addAttribute("lessee",lessee);
        return "lesseeUpdate";
    }

    //修改租户信息
    @RequestMapping("/update")
    public String updateLessee(Lessee lessee){
        int i = lesseeService.updateLeessee(lessee);
        if (i!=0){
            return "redirect:/lessee/getAll";
        }
        return "lesseeError";
    }
    //删除租户
    @RequestMapping("/delete")
    public String deleteLessee(Integer id){
        //查询租户是否存在
        List<Lessee> lesseeList = lesseeService.selectLesseeIsZuFangIng(id);
        if (lesseeList.size()!=0){
            return "redirect:/lessee/getAll";
        }
        lesseeService.deleteLessee(id);
        return "redirect:/lessee/getAll";
    }
    //跳转到新增界面
    @RequestMapping("/doAddLessee")
    public String doAddLessee(){
        return "doAddLessees";
    }

    //新增租户
    @RequestMapping("/addLessee")
    public String addLessee(Lessee lessee){
        int i=lesseeService.addLessee(lessee);
        if (i!=0){
            return "redirect:/lessee/getAll";
        }
        return "lesseeError";
    }
}
